﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Mail;

namespace UtilsLib.UtilsMail
{
    public class MailUtility
    {
        //Fields
        private SmtpClient _smtpClient;

        //Properties
        public SmtpClient SmtpClient
        {
            get { return _smtpClient; }
            set { _smtpClient = value; }
        }

        //Constructors
        public MailUtility(string host)
        {
            _smtpClient = new SmtpClient(host);
        }
        public MailUtility(SmtpClient client)
        {
            _smtpClient = client;
        }

        //Mail Sending Functions
        public void SendMail(MailAddress[] recipients, MailAddress sender, string body)
        {
            SendMail(recipients, sender, body, null, null, _smtpClient);
        }
        public void SendMail(MailAddress[] recipients, MailAddress sender, string body, string subject)
        {
            SendMail(recipients, sender, body, subject, null, _smtpClient);
        }
        public void SendMail(MailAddress[] recipients, MailAddress sender, string body, AttachmentCollection attachments)
        {
            SendMail(recipients, sender, body, null, attachments, _smtpClient);
        }
        public void SendMail(MailAddress[] recipients, MailAddress sender, string body, string subject, AttachmentCollection attachments)
        {
            SendMail(recipients, sender, body, subject, attachments, _smtpClient);
        }

        //Static Mail Sending Functions
        public static void SendMail(MailAddress[] recipients, MailAddress sender, string body, SmtpClient client)
        {
            SendMail(recipients, sender, body, null, null, client);
        }
        public static void SendMail(MailAddress[] recipients, MailAddress sender, string body, string subject, SmtpClient client)
        {
            SendMail(recipients, sender, body, subject, null, client);
        }
        public static void SendMail(MailAddress[] recipients, MailAddress sender, string body, AttachmentCollection attachments, SmtpClient client)
        {
            SendMail(recipients, sender, body, null, attachments, client);
        }

        public static void SendMail(MailAddress recipient, MailAddress sender, string body, string subject, SmtpClient client)
        {
            SendMail(new MailAddress[] { recipient }, sender, body, subject, null, client);
        }
        public static void SendMail(MailAddress recipient, MailAddress sender, string body, string subject, AttachmentCollection attachments, SmtpClient client)
        {
            SendMail(new MailAddress[] { recipient }, sender, body, subject, attachments, client);
        }
        public static void SendMail(MailAddress[] recipients, MailAddress sender, string body, string subject, AttachmentCollection attachments, SmtpClient client)
        {
            if (recipients == null || recipients.Count() == 0)
                throw new Exception("No recipients defined.");
            if (string.IsNullOrEmpty(body))
                throw new Exception("Body is empty.");

            MailMessage message = new MailMessage();

            foreach (MailAddress rec in recipients)
                message.To.Add(rec);

            if (attachments != null && attachments.Count > 0)
                foreach (Attachment item in attachments)
                    message.Attachments.Add(item);

            message.Subject = subject ?? "";
            message.From = sender;
            message.Body = body;
            client.Send(message);
        }
    }
}
